const express = require('express');
const createError = require('http-errors');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const session=require('express-session');//session引入

const authControll = require('./utils/authControll');

// let indexRouter = require('./routes/index');
// let usersRouter = require('./routes/users');
// let wf_systemHomepageRouter=require('./routes/wf_systemHomepage');
// var studentRouter = require('./routes/student');
const adminRouter = require('./routes/administrators')

const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users')
const systemHomepageRouter=require('./routes/systemHomepage');
const studentRouter = require('./routes/student');
const courseRouter = require('./routes/courses')
const perSetRouter = require('./routes/perSet')
const booksRouter = require('./routes/books');
const msgListRouter = require('./routes/msgList')

//引用教师
const teacherRouter = require('./routes/teachers');
const favicon = require('serve-favicon');

//exprees实例化
var app = express();



// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));


app.use(cookieParser());
app.use(session({
  secret: 'rtdfyguhasijdkljskflsdsdfkhjsdhgkjshdfjkg',//对sessionId的签名，越复杂越好，防止篡改
  name: 'myApp',//在浏览器端生成的cookie 的名字
  cookie: {//cookie配置
    // httpOnly: true,//只能在http下使用，暂时不需要
    maxAge: 1000 * 60*60,//cookie 的过期时间设置
  },
  // resave: false,
  // saveUninitialized: true,
  // store,//session存储方式，默认放在内存中，也可以设置在mySQL等数据或者 redis 中，可以不做配置
  rolling: true,//每次请求时强行设置cookie，就会重置cookie 的过期时间

}));




//路由拦截
// 排除users/login接口，排除路径login.html，拦截
app.use(authControll);

//静态资源
app.use(express.static(path.join(__dirname, 'public')));

//配置网页图标
app.use(favicon(path.join(__dirname,'public','images','Logo.png')))






//路由 -接收请求的
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/teachers', teacherRouter);
app.use('/student', studentRouter);
app.use('/administrators',adminRouter)
app.use('/wf_systemHomepage', systemHomepageRouter);
app.use('/courses',courseRouter)
app.use('/perSet', perSetRouter)
app.use('/books', booksRouter);
app.use('/msgList',msgListRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;